/****

    activequant - activestocks.eu

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

	
	contact  : contact@activestocks.eu
    homepage : http://www.activestocks.eu

****/
package org.activequant.core.types;

import java.io.Serializable;

import org.activequant.core.util.TimeStampFormat;
import org.activequant.util.tools.IdentityUtils;


/**
 *  Expiry type : encapsulates year, month and date of option expiry (strips off hour/minute/etc details).<br>
 * Holds the following associated variable:
 *	<ul>
 *  <li>stamp(TimeStamp)</li>
 *  </ul>
 * <br>
 * <b>History:</b><br>
 *  - [19.10.2007] Created (Mike Krotikov)<br>
 *  - [23.10.2007] Adding days to simple date format (Ulrich Staudinger)<br>
 *  - [07.11.2007] Fixed days/month parsing (Erik Nijkamp)<br/> 
 *  @author Mike Kroutikov
 */
public final class Expiry implements Comparable<Expiry>, Serializable {

	private static final long serialVersionUID = -4281552011324401469L;
	/**
	 * private static final TimeStampFormat FMT_LONG = new TimeStampFormat("yyyyMMdd");
	 */
	private static final TimeStampFormat FMT_LONG = new TimeStampFormat("yyyyMMdd");
	/**
	 * holds the associated stamp(TimeStamp)
	 */
	private final TimeStamp stamp;
	/**
	 * constructs an Expiry whose stamp(TimeStamp) is set with the current time
	 */
	public Expiry() {
		stamp = new TimeStamp();
	}
	/**
	 * constructs an Expiry whose stamp(TimeStamp) is set with the stamp(TimeStamp) of the given other(Expiry)
	 * @param other
	 */
	public Expiry(Expiry other) {
		stamp = other.stamp;
	}
	/**
	 * constructs an Expiry whose stamp(TimeStamp) is set by normalizing the given timeStamp(TimeStamp) (strips off hour/minute/etc details)
	 * @param timeStamp
	 */
	public Expiry(TimeStamp timeStamp) {
		// this normalizes the date (strips off hour/minute/etc details)
		this(FMT_LONG.format(timeStamp));
	}
	
	/**
	 * constructs an Expiry whose stamp(TimeStamp) is parsed from the given text(String) (requires yyyyMMdd format)
	 * @param text format has to be yyyyMMdd
	 */
	public Expiry(String text) {
		stamp = parseExpiryDate(text);
	}
	/**
	 * returns the associated stamp(TimeStamp)
	 * @return
	 */
	public TimeStamp getTimeStamp() { return stamp; }
	/**
	 * returns the associated stamp(TimeStamp) as a String formatted with the "yyyyMMdd" format
	 */
	public String toString() {
		return FMT_LONG.format(this.stamp);
	}
	/**
	 * parses a TimeStamp from the given text(String) (requires yyyyMMdd format)
	 * @param text
	 * @return
	 */
	private static TimeStamp parseExpiryDate(String text) {
		return FMT_LONG.parse(text);
	}
	/**
	 * compares the associated stamp(TimeStamp) with the stamp(TimeStamp) of the given other(Expiry)
	 */
	public int compareTo(Expiry other) {
		return this.stamp.compareTo(other.stamp);
	}
	/**
	 * returns the hashCode(int) of the associated stamp(TimeStamp)
	 */
	public int hashCode() {
		return this.stamp.hashCode();
	}
	/**
	 * returns whether the given other(Object) and this Expire are equal (either the same object or the given other(Object)
	 * is an Expiry object whose stamp(TimeStamp) is equal to the associated stamp(TimeStamp) of this Expiry object)
	 */
	public boolean equals(Object other) {
		return IdentityUtils.equalsTo(this, other);
	}
}
